/**
 * 数据协议约定
 */
export type BaseMaterialType = {
    id?: string
    parentId?: string
    type: string
    // 用于区分物料模型、物料实例
    isInstance: boolean
}

export type TextMaterialType = BaseMaterialType & {
    type: 'text'
    props?: {
        content: string
    }
    actions?: {
        onCopy: () => void
        onEdit: () => void
    }
}

export type ImageMaterialType = BaseMaterialType & {
    type: 'image'
    props?: {
        src: string
        alt: string
    }
    actions?: {
        onClick: () => void
        onLoad: () => void
    }
}

export type ChartMaterialType = BaseMaterialType & {
    type: 'chart'
    props?: {
        src: string
        alt: string
    }
    actions?: {
        onClick: () => void
        onLoad: () => void
    }
}

export type ContainerMaterialType = BaseMaterialType & {
    type: 'container'
    children?: BaseMaterialType[]
    props?: {
        src: string
        alt: string
    }
    actions?: {
        onClick: () => void
        onLoad: () => void
    }
}

export type MaterialType = TextMaterialType | ImageMaterialType | ChartMaterialType | ContainerMaterialType;

// 类型保护和推导、断言
const isImage = (material: MaterialType): material is ImageMaterialType => {
    return material.type === 'image';
}

/*
const b: MaterialType = {
    type: 'text'
};
if(isImage(b)){
    b.type = 'image';
}
*/
